package com.qdlc.p2p.manage.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.RedEnvelopeService;
import com.qdlc.p2p.biz.service.UserVipLevelService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.dto.RedEnvelope;
import com.qdlc.p2p.dal.dto.UserVipLevel;

/**
 * vip等级管理
 * 
 * @author zf
 * @version 2.0
 */
public class ManageUserVipLevelAction extends BaseAction<UserVipLevel> implements ModelDriven<UserVipLevel> {

	@Resource
	private UserVipLevelService vipLevelService;

	private Map<String, Object> data = new HashMap<String, Object>();

	@Resource
	private RedEnvelopeService redEnvelopeService;
	
	/**
	 * vip等级管理
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/modules/user/vip/vipLevelManager")
	public String vipLevelManager() throws Exception {
		return "vipLevelManager";
	}

	/**
	 * vip等级列表
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/modules/user/vip/vipLevelList")
	public void vipLevelList() throws Exception {
		List<UserVipLevel> list = vipLevelService.getAllLevel();
		data.put("total", 1);
		data.put("rows", list);
		printJson(getStringOfJpaObj(data));
	}
	
	/**
	 * vip等级添加页面
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/modules/user/vip/addVipLevelPage")
	public String addVipLevelPage() throws Exception {
		List<RedEnvelope> redEnvelopeList = redEnvelopeService.findUsableRedEnvelopeForFixed();
		request.setAttribute("redEnvelopeList", redEnvelopeList);
		return "addVipLevelPage";
	}
	/**
	 * vip等级添加
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/modules/user/vip/addVipLevel")
	public void addVipLevel() throws Exception {
		model.setOperator(getOperator());
		
		String redEnvelopeRule = paramString("redEnvelopeRule");
		if(StringUtil.isNotBlank(redEnvelopeRule)){
			String str[] = redEnvelopeRule.split(",");
			if(str.length>=2){
				long redEnvelopeRuleId = Long.parseLong(str[0]);
				double giftMoney = Long.parseLong(str[1]);
				model.setRedEnvelopeRuleId(redEnvelopeRuleId);
				model.setGiftMoney(giftMoney);
			}
		}
		vipLevelService.save(model);
		printSuccess();
	}
	/**
	 * vip等级修改页面
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/modules/user/vip/updateVipLevelPage")
	public String updateVipLevelPage() throws Exception {
		UserVipLevel vipLevel = vipLevelService.findById(model.getId());
		List<RedEnvelope> redEnvelopeList = redEnvelopeService.findUsableRedEnvelopeForFixed();
		request.setAttribute("vipLevel", vipLevel);
		request.setAttribute("redEnvelopeList", redEnvelopeList);
		return "updateVipLevelPage";
	}
	/**
	 * vip等级修改
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/modules/user/vip/updateVipLevel")
	public void updateVipLevel() throws Exception {
		model.setOperator(getOperator());
		String redEnvelopeRule = paramString("redEnvelopeRule");
		if(StringUtil.isNotBlank(redEnvelopeRule)){
			String str[] = redEnvelopeRule.split(",");
			if(str.length>=2){
				long redEnvelopeRuleId = Long.parseLong(str[0]);
				double giftMoney = Long.parseLong(str[1]);
				model.setRedEnvelopeRuleId(redEnvelopeRuleId);
				model.setGiftMoney(giftMoney);
			}
		}
		UserVipLevel userVipLevel = vipLevelService.findById(model.getId());
		model.setAddTime(userVipLevel.getAddTime());
		vipLevelService.update(model);
		printSuccess();
	}
	/**
	 * 删除vip等级
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/modules/user/vip/deleteVipLevel")
	public void deleteVipLevel() throws Exception {
		UserVipLevel vipLevel = vipLevelService.findById(model.getId());
		vipLevel.setOperator(getOperator());
		vipLevelService.delete(vipLevel);
		printSuccess();
	}
}
